package leetcode.medium.limit100;

import leetcode.ListNode;

public class No82 {
    public static void main(String[] args) {
        ListNode listNode = ListNode.createByArray(new int[]{1, 2, 2});
        No82 no82 = new No82();
        ListNode.print(no82.deleteDuplicates(listNode));
    }

    public ListNode deleteDuplicates(ListNode head) {
        ListNode fast = head;
        ListNode node = new ListNode(-1);
        ListNode slow = node;
        while (fast != null) {
            if ((fast.next != null && fast.val == fast.next.val)) {
                while (fast.next != null && fast.val == fast.next.val) {
                    fast = fast.next;
                }
                fast = fast.next;
                if (fast == null) {
                    slow.next = null;
                }
            } else {
                slow.next = fast;
                slow = slow.next;
                fast = fast.next;
            }
        }
        return node.next;
    }
}
